<%@ page import="Apr06.pojo.News" %>
<%@ page import="Apr06.NewsService.NewsService" %>
<%@ page import="Apr06.NewsServiceImpl.NewsServiceImpl" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.net.URLEncoder" %>
<%--
  Created by IntelliJ IDEA.
  User: wu
  Date: 2022/4/7
  Time: 14:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>接收数据并处理</title>
</head>
<body>
    <%

        //接收数据
        request.setCharacterEncoding("utf-8");


        boolean filr= ServletFileUpload.isMultipartContent(request);
        //准备上传的文件地址
        String path="D:\\KH-38\\upload";
        File file=new File(path);
        //如果目录不存在，就创建目录
        if(!file.exists()){
            file.mkdir();
        }
        //判断文件表单提交是否正确
        if (filr){
            //创建文件上传的核心工厂
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //文件上传对象
            ServletFileUpload servletFileUpload  = new ServletFileUpload(fileItemFactory);
            //获取表单的文件对象
            List<FileItem> list = servletFileUpload.parseRequest(request);
            //第一大步：上传数据
            Iterator<FileItem> iterator= list.iterator();

            String fieldTitle=null;
            String picPath=null;
            int id=0;
            int categoryId=0;



            while (iterator.hasNext()){
                FileItem fileUpload=iterator.next();
                boolean isfile =  fileUpload.isFormField();
                if(isfile){
                    String  fieldName=  fileUpload.getFieldName();
                    if (fieldName.equals("id")){
                        id=Integer.parseInt(fileUpload.getString("utf-8"));
                    }else if (fieldName.equals("categoryId")){
                        categoryId=Integer.parseInt(fileUpload.getString("utf-8"));
                    }else if(fieldName.equals("title")){
                        fieldTitle= fileUpload.getString("utf-8");
                    }
                }else {
                    //文件名称
                    String name= URLEncoder.encode(fileUpload.getName(),"utf-8") ;
                    File file1 = new File(name);
                    if(file1.getName() !=null && !(file1.getName().equals(""))){
                        //将对应的文件上传到指定的值
                        File file2 = new File(path+"\\"+URLEncoder.encode(file1.getName(),"utf-8"));
                        //完成文件写入
                        fileUpload.write(file2);
                        picPath=URLEncoder.encode(file2.getName(),"utf-8");
                    }
                }
            }
            //第二大步：保存数据库信息
            News news=new News();
            news.setId(id);
            news.setCategoryId(categoryId);
            news.setTitle(fieldTitle);
            news.setPicPath(path+"\\"+picPath);

            NewsService newsService=new NewsServiceImpl();
            int cont=newsService.ServiceInsertNews1(news);
            if (cont>0){
                request.getRequestDispatcher("news_detail.jsp").forward(request,response);
            }else {
                out.print("上传失败");
            }
        }

    %>

</body>
</html>
